<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<script type="text/javascript"
        src="<%=request.getContextPath()%>/js/business/trail.js"></script>
<script type="text/javascript"
        src="<%=request.getContextPath()%>/js/business/book.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/hint/jquery.hint.js"></script>
<script type="text/javascript">
    var checkDetail = $("#checkDetailInput").val();

    if (checkDetail == "T") {
        $(function () {
            $(":text").attr("readOnly", "readOnly");
            $("#add").hide();
            $("#del").hide();
            $("#queren").hide();
            $("#li1Select").attr("onmousemove", "this.setCapture();");
            $("#li1Select").attr("onmouseout", "this.releaseCapture();");
            $("#li1Select").attr("onfocus", "this.blur();");
        });
    }

    var book_gfDate_val = $("#book_gfDate").val();
    var book_gfDate = book_gfDate_val.substring(0, 10);
    $("#book_gfDate").val(book_gfDate);
    // var book_shipDate_val = $("#book_shipDate").val();
    // var book_shipDate = book_shipDate_val.substring(0,10);
    // $("#book_shipDate").val(book_shipDate);

</script>
<style>
    #containers1 .li1 input {
        width: 30px;
    }
</style>
<div class="page_font">
    <ul class="ul1">
        <!-- 		<li class="li1" style="font-size: 14px; padding-top: 4px;">订舱业务代理人：</li> -->
        <!-- 		<li class="li1"><input type="text" id="book_sup_id" -->
        <%-- 			name="book.supCompany.name" value="${book.supCompany.name}" /><input type="hidden" --%>
        <%-- 			id="book_sup" name="book.supCompany.id" value="${book.supCompany.id }" /></li> --%>
        <li class="li1 first_li">订舱业务代理人：</li>
        <li class="li1"><input type="text" id="book_boat_id"
                               name="book.supCompany.name" value="${book.supCompany.name }"/><input
                type="hidden" id="book_boat" name="book.supCompany.id"
                value="${book.supCompany.id }"/></li>
    </ul>
    <ul class="ul1">
        <li class="li1 first_li">PI号：</li>
        <li class="li1"><input
                type="text" id="book_pi" name="book.pi"
                value="${book.pi }" onblur="javascript:getPi(this.value);"/></li>
    </ul>
    <table>
        <tr>
            <td class="first_li">货物信息填写：</td>
        </tr>
    </table>
    <div id="newGoods" class="bizMsg">
        <div>
            <ul>
                <li class="li1">货物名称：</li>
                <li class="li1"><input type="text" id="goodsName"/></li>
                <li class="li1 li1_space">每柜毛重：</li>
                <li class="li1"><input type="text" id="weight"/>KG</li>
                <li class="li1 li1_space">体积：</li>
                <li class="li1"><input type="text" id="volume"/>M³</li>
            </ul>
            <div class="clean"></div>
        </div>
        <div id="book">
            <input value="${book.id}" type="hidden" name="book.id"/>
            <input value="${checkDetail}" type="hidden" id="checkDetailInput"/>
            <ul>
                <li class="li1 li1_Special">柜量：</li>
            </ul>
            <ul id="containers1">
                <li class="li1"><span class='containerType1'><input
                        id="ct_num1" type="text" class="ct_num1"/><input id="ct_size1"
                                                                         type="text"
                                                                         class="ct_size1"/><input
                        id="ct_type1" type="text"
                        class="ct_type1"/></span></li>
                <li><input type="button" onclick="addCtbookType()" value="添加柜型" id="add"/>
                    <input type="button" onclick="delCtBookType();" id="delbookType" id="del"
                           value="删除柜型">
                    <!--<input type="button" onclick="showTrailInfo();" value="确认柜量" /></li>-->
                    <input type="hidden" id="isModify"/> <input type="hidden"
                                                                id="temp_goodsId"/> <input
                            type="button" value="确认货物"
                            id="queren"
                            onclick="confirm();"/>
                    <input id="clearPkInput" value="${clearPk}" type="hidden"/>
            </ul>
        </div>
    </div>

    <table>
        <tr>
            <td class="first_li">货物列表：</td>
        </tr>
    </table>
    <table class="dataTB dataTB_Individual booking_dataTB_Individual "
           id="goodsList">

        <tr>
            <td class="booking_titleTD" lang="name">货物名称</td>
            <td class="booking_titleTD" lang="gweight">每柜毛重</td>
            <td class="booking_titleTD" lang="volume">体积</td>
            <td class="booking_titleTD" lang="ctNumType">柜型</td>
            <td style="display: none;" lang="id"></td>
            <td class="booking_titleTd">操作</td>
        </tr>
        <!-- 		class="pointer" -->
        <c:forEach items="${book.goodses}" varStatus="st">
            <tr id="goodsList_${st.index}"
                onclick="edit(this);">
                <td><c:out value="${name}"/></td>
                <td><c:out value="${volume}"/></td>
                <td><c:out value="${volume}"/></td>
                <td><c:out value="${ctNumType}"/></td>
                <c:if test="${clearPk==1}">
                    <td style="display: none;"><c:out value="${st.index}"/></td>
                </c:if>
                <c:if test="${clearPk!=1}">
                    <td style="display: none;"><c:out value="${id}"/></td>
                </c:if>
                <td><a style='cursor: pointer;color: blue;margin: 10px;'
                       onclick="delBookBusi(this);">删除</a></td>
            </tr>
        </c:forEach>
    </table>
    <br/>
    <!-- 	<table> -->
    <!-- 		<tr> -->
    <!-- 			<td class="first_li">收货人信息：</td> -->
    <!-- 		</tr> -->
    <!-- 	</table> -->
    <!-- 	<div class="bizMsg"> -->
    <!-- 		<div> -->
    <!-- 			<ul> -->
    <!-- 				<li class="li1">收货公司名称：</li> -->
    <!-- 				<li class="li1"><input id="arr_company" type="text" -->
    <%-- 					name="book.consignee.company" value="${ book.consignee.company}" /><input --%>
    <!-- 					type="hidden" id="arr_company_id" name="book.consignee.id" -->
    <%-- 					value="${book.consignee.id }" /></li> --%>
    <!-- 				<li class="li1 li1_space">联系人：</li> -->
    <!-- 				<li class="li1"><input type="text" id="arr_con_name" -->
    <%-- 					name="book.consignee.name" value="${book.consignee.name }" /></li> --%>
    <!-- 				<li class="li1 li1_space">联系电话：</li> -->
    <!-- 				<li class="li1"><input type="text" id="arr_con_phone" -->
    <%-- 					name="book.consignee.phone" value="${book.consignee.phone }" /></li> --%>
    <!-- 			</ul> -->
    <!-- 		</div> -->
    <!-- 	</div> -->
    <table>
        <tr>
            <td class="first_li" style="padding-top: 10px;">托运信息：</td>
        </tr>
    </table>
    <div class="bizMsg">
        <div>
            <table>
                <ul class="ul1">
                    <!-- 			<li class="li1">订舱业务代理人：</li> -->
                    <!-- 			<li class="li1"><input type="text" id="book_boat_id" -->
                    <%-- 			name="book.supCompany.name" value="${book.supCompany.name }" /><input --%>
                    <!-- 			type="hidden" id="book_boat" name="book.supCompany.id" -->
                    <%-- 			value="${book.supCompany.id }" /></li> --%>
                    <tr>
                        <td>
                            <li class="li1 li1_space" style="float:right;">大船公司：</li>
                        </td>
                        <td>
                            <li class="li1"><input type="text" id="book_ship_id"
                                                   name="book.shipping.name"
                                                   value="${book.shipping.name }"/><input
                                    type="hidden" id="book_ship" name="book.shipping.id"
                                    value="${book.shipping.id }"/></li>
                        </td>
                        <td>
                            <li class="li1 li1_space">成交方式：</li>
                        </td>
                        <td>
                            <li class="li1"><select id="li1Select" class="select_width"
                                                    name="book.businessType">
                                <%--<s:set id="businessType" value="book.businessType"/>--%>
                                <%--<s:iterator value="businessType" status="status">--%>
                                <%--<option--%>
                                <%--value='<s:property value="businessType[#status.index][1]"/>'--%>
                                <%--<s:if test="#businessType==null && businessType[#status.index][1]=='CIF'">selected="selected"</s:if>--%>
                                <%--<s:elseif--%>
                                <%--test="businessType[#status.index][1]==#businessType ">selected="selected"</s:elseif>--%>
                                <%-->--%>
                                <%--<s:property value="businessType[#status.index][0]"/>--%>
                                <%--</option>--%>
                                <%--</s:iterator>--%>
                                <c:set var="businessType" value="book.businessType"/>
                                <c:forEach items="${businessType}" varStatus="status">
                                    <option
                                            value='${businessType[status.index][1]}"'
                                            <c:if test="${businessType==null && businessType[status.index][1]=='CIF'}">selected="selected"</c:if>
                                            <c:if
                                                    test="${businessType[status.index][1]==businessType}">selected="selected"</c:if>
                                    >
                                            ${businessType[status.index][0]}
                                    </option>
                                </c:forEach>

                            </select></li>
                        </td>
                        <!-- 			</ul> -->
                        <!-- 		</div> -->
                        <!-- 		<div> -->
                        <!-- 			<ul class="ul1"> -->
                        <td>
                            <li class="li1"
                                style="padding-left: 42px; letter-spacing: 2px;float:right;">起运港：
                            </li>
                        </td>
                        <td>
                            <li class="li1"><input type="text" id="stPort_book" value=""/><input
                                    type="hidden"
                                    name="book.stPort"
                                    id="stPort_book_id"
                                    value="<c:out value='${book.stPort}' />"/>
                            </li>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <li class="li1" style="padding-left: 42px; letter-spacing: 2px;">目的港：
                            </li>
                        </td>
                        <td>
                            <li class="li1"><input type="text" id="arPort_book" value=""/><input
                                    type="hidden"
                                    name="book.arPort"
                                    id="arPort_book_id"
                                    value="<c:out value='${book.arPort}' />"/>
                            </li>
                        </td>
                        <td>
                            <li class="li1 li1_space">船期要求：</li>
                        </td>
                        <td>
                            <li class="li1"><input type="text" id="book_shipDate"
                                                   name="book.shipDate" value="${book.shipDate }"/>
                            </li>
                        </td>
                        <!-- 			</ul> -->
                        <!-- 		</div> -->
                        <!-- 		<div> -->
                        <!-- 			<ul class="ul1"> -->
                        <td>
                            <li class="li1"
                                style="padding-left: 42px; letter-spacing: 2px; float:right;">总柜型柜量：
                            </li>
                        </td>
                        <td>
                            <li class="li1">
                                <!-- 					<input style="width: 96px;" type="text" -->
                                <!-- 						readonly="readonly" name="book.ctNumType" -->
                                <%-- 						value="${book.ctNumType }" />  <span class='containerType2'> --%>
                                <input id="bookTotalType"
                                       name="book.ctNumType" type="text" readonly="readonly"
                                       class='containerType2'
                                       value="${book.ctNumType}"/>
                                <%-- 				</span> --%>
                            </li>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <li class="li1 li1_space" style="padding-left: 3px;float:right;">合作价格：
                            </li>
                        </td>
                        <td>
                            <li class="li1"><input type="text" name="book.price"
                                                   value="${book.price }"/></li>
                        </td>
                        <td>
                            <li class="li1 li1_space" style="padding-left: 3px; float:right;">
                                货好时间：
                            </li>
                        </td>
                        <td>
                            <li class="li1">
                                <input type="text" id="book_gfDate" name="book.gfDate"
                                       value="${book.gfDate }"/>
                            </li>
                        </td>
                        <!-- 			</ul> -->
                        <!-- 		</div> -->
                        <!-- 		<div> -->
                        <!-- 			<ul class="ul1"> -->
                        <td>
                            <li class="li1 li1_space"
                                style="padding-left: 42px; letter-spacing: 2px;"></li>
                        </td>
                        <td>
                            <li class="li1">
                                <%-- 					<input type="text" id="book_gfDate" name="book.gfDate" value="${book.gfDate }" /> --%>
                            </li>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <li class="li1 li1_space" style="padding-left: 22px;">备注：</li>
                        </td>
                        <td colspan="5">
                            <li class="li1" style="width: 100%;"><input type="text"
                                                                        style="width: 100%;"
                                                                        name="book.remark"
                                                                        value="${book.remark }"/>
                            </li>
                        </td>
                    </tr>
                </ul>
            </table>
        </div>
    </div>
    <div class="clean"></div>
    <div id="book_input" style="display: none;"></div>
    <br/>
    <div id="so">
        <table>
            <c:if test="book.soDoc!=null">
                <tr>
                    <td class="first_li">SO信息：</td>
                </tr>
                <tr>
                    <td>${book.soDoc.name}&nbsp;&nbsp;<input type="button"
                                                             value="点击下载SO文件"
                                                             onclick="javascript:window.location.href='download?attachment.id=${book.soDoc.id}'"/>
                    </td>
                </tr>
            </c:if>
        </table>
    </div>
</div>
<script type="text/javascript">
    //收货公司名称
    $(function () {
        $("#arr_company").autocomplete({
                                           source: function (request, response) {
                                               $.ajax({
                                                          url: "contacter-findListByName",
                                                          dataType: "json",
                                                          type: "post",
                                                          data: {name: request.term},
                                                          success: function (data) {
                                                              response($.map(data.contacters,
                                                                             function (item) {
                                                                                 return {
                                                                                     id: item.id,
                                                                                     username: item.name,
                                                                                     phone: item.phone,
                                                                                     label: item.name
                                                                                            + " - "
                                                                                            + item.company,
                                                                                     value: item.company,
                                                                                     address: item.address
                                                                                 };
                                                                             }));
                                                          }
                                                      });
                                           }, minLength: 1, select: function (event, ui) {
                $("#arr_company_id").val(ui.item.id);
                $("#arr_con_name").val(ui.item.username);
                $("#arr_con_phone").val(ui.item.phone);
            }
                                       });
    });
    //订舱代理人
    $(function () {
        $("#book_boat_id").autocomplete({
                                            source: function (request, response) {
                                                $.ajax({
                                                           url: "business-findCompanysByNameType",
                                                           dataType: "json",
                                                           type: "post",
                                                           data: {
                                                               com_type: "CC",
                                                               com_idefined: "bookingCompany",
                                                               sup_type: "F",//订舱业务类型
                                                               com_name: request.term
                                                           },
                                                           success: function (data) {
                                                               response($.map(data.companys,
                                                                              function (item) {
                                                                                  return {
                                                                                      id: item.id,
                                                                                      name: item.name,
                                                                                      value: item.name
                                                                                  };
                                                                              }));
                                                           }
                                                       });
                                            },
                                            minLength: 0,
                                            select: function (event, ui) {
                                                $("#book_boat").val(ui.item.id);
                                            },
                                            change: function (event, ui) {
                                                var book_boat_id = $("#book_boat_id").val();
                                                $.ajax({
                                                           url: "business-findCompanysByNameTypeIsRight",
                                                           dataType: "json",
                                                           type: "post",
                                                           data: {
                                                               com_type: "CC", sup_type: "F",//订舱业务类型
                                                               com_name: book_boat_id
                                                           },
                                                           success: function (data) {
                                                               if (data.companys == "") {
                                                                   alert("请在订舱业务代理人下拉框中选择订舱业务代理人!");
                                                                   $("#book_boat_id").focus();
                                                               } else {
                                                                   var book_boat_id = $(
                                                                           "#book_boat_id").val();
                                                                   var book_boat = $(
                                                                           "#book_boat").val();
                                                                   if (book_boat_id != ""
                                                                       && book_boat == "") {
                                                                       alert("请在订舱业务代理人下拉框中选择订舱业务代理人!");
                                                                       $("#book_boat_id").focus();
                                                                   }
                                                               }
                                                           }
                                                       });
                                            }
                                        });
        $("#book_boat_id").bind("click", function () {
            $(this).autocomplete('search', '');
        });
    });
    //大船公司, com_idefined:"bookingCompany"
    $(function () {
        $("#book_ship_id").autocomplete({
                                            source: function (request, response) {
                                                $.ajax({
                                                           url: "business-findCompanysByNameType",
                                                           dataType: "json",
                                                           type: "post",
                                                           data: {
                                                               com_type: "SC",
                                                               com_name: request.term
                                                           },
                                                           success: function (data) {
                                                               response($.map(data.companys,
                                                                              function (item) {
                                                                                  return {
                                                                                      id: item.id,
                                                                                      name: item.name,
                                                                                      value: item.name
                                                                                  };
                                                                              }));
                                                           }
                                                       });
                                            },
                                            minLength: 1,
                                            select: function (event, ui) {
                                                $("#book_ship").val(ui.item.id);

                                            },
                                            change: function (event, ui) {
                                                var book_ship_id = $("#book_ship_id").val();
                                                $.ajax({
                                                           url: "business-findCompanysByNameTypeIsRight",
                                                           dataType: "json",
                                                           type: "post",
                                                           data: {
                                                               com_type: "SC",
                                                               com_name: book_ship_id
                                                           },
                                                           success: function (data) {
                                                               if (data.companys == "") {
                                                                   alert("请在大船公司下拉框中选择大船公司!");
                                                               }
                                                           }
                                                       });
                                            }
                                        });
        $("#book_ship_id").bind("click", function () {
            $(this).autocomplete('search', '');
        });
    });
    $(function () {
        //船期要求
// 		$("#book_shipDate").datepicker();
        //货好时间
        $("#book_gfDate").datepicker();
    });

    //删除订舱列表,当为查看详细页面的时候不能删除
    //用来阻止<tr>点击事件的编辑edit(this)
    var stopCopy = 1;
    function delBookBusi(obj) {
        if (checkDetail == "T") {
            return;
        }
        $(obj).parents("tr").remove();
        stopCopy = 1;
    }

    function isNull(value) {
        if (!value) {
            return "undefined";
        } else {
            return value;
        }
    }

    function isUndefined(value) {
        value = $.trim(value);
        if (value.indexOf("undefined") == 0) {
            return "";
        } else if (value == "0") {
            return "";
        } else {
            return value;
        }
    }

    //截取0至fromIndex前的字符串
    function subStringBySamble(value, punctuation, fromIndex) {
        if (value.indexOf(punctuation, fromIndex) > -1) {
            return value.substring(0, fromIndex);
        } else {
            return value;
        }
    }

    //检查订舱单是否有货物名和柜型都相同的货物
    function sameBooking() {
        //已经确定的货物和柜型
        var trs = $("#goodsList").find("tr");
        var goodsArr = new Array();
        var cabinetArr = new Array();
        if (trs.length > 1) {
            for (var i = 1; i < trs.length; i++) {
                var goodsName = trs.eq(i).find("td").eq(0).text();
                goodsArr = goodsArr.concat(goodsName);
                var cabinet = trs.eq(i).find("td").eq(3).text() + "";
                //对取到的柜型进行处理，只取柜尺寸和柜型
                cabinet = cabinet.replace("/", "X");
                var tempArr = cabinet.split("X");
                cabinet = "";
                var tempArr1 = new Array();
                for (var j = 1, n = 0; j < tempArr.length; j += 2, n++) {
                    tempArr1[n] = tempArr[j];
                }
                tempArr1.sort();
                cabinet = tempArr1.join("");
                cabinetArr = cabinetArr.concat(cabinet);
            }
        }
        //等待确认的货物和柜型
        var goodsName1 = $("input[id=goodsName]").val();
        var cabinetArr1 = new Array();
        var ctSize = $("#containers1 .li1 .ct_size1");
        var cabinet1 = "";
        for (var i = 0; i < ctSize.length; i++) {
            cabinet1 = ctSize.eq(i).val() + "'" + $("#containers1 .li1 .ct_type1").eq(i).val();
            cabinetArr1 = cabinetArr1.concat(cabinet1);
        }
        cabinetArr1.sort();
        cabinet1 = cabinetArr1.join("");
        //进行比较  货物和柜型都相同的则返回false，反之true
        for (var i = 0; i < goodsArr.length; i++) {
            if (goodsArr[i] == goodsName1 && cabinetArr[i] == cabinet1) {
                return true;
            }
        }
        return false;
    }
    //添加订舱货物信息
    var temp_goodsId = 0;
    function confirm() {
        var goodsName = $("#goodsName").val();
        var weight = $("#weight").val();
        var volume = $("#volume").val();
        var model = "";
        var id = $("#temp_goodsId").val();
        var clearPk = $("#clearPkInput").val();
        if (clearPk == 1 && (id != null && id != "undefined" && id != "")) {
            temp_goodsId = $("#goodsList tr").length - 1;
            $("#temp_goodsId").val(temp_goodsId);
            clearPk = $("#clearPkInput").val(0);
        }
        /*var model1 = $("#model1").val();
         var model2 = $("#model2").val();
         var model3 = $("#model3").val();
         var model4 = $("#model4").val();
         if ((model1 == null || model1 == "")
         && (model2 == null || model2 == "")
         && (model3 == null || model3 == "")
         && (model4 == null || model4 == "")) {
         alert("请填写柜型!");
         return;
         }
         model = isUndefined(isNull(model1) + "X20'HD/")
         + isUndefined(isNull(model2) + "X40'/")
         + isUndefined(isNull(model3) + "X40'HQ/")
         + isUndefined(isNull(model4) + "X45'");
         model = subStringBySamble(model, "/", model.length - 1);*/

        var cons = $("#containers1 .li1 .containerType1");
        if (cons && cons.length > 0) {
            $
                    .each(cons, function (i, v) {
                        var num = $(this).find(".ct_num1").val();
                        var size = $(this)
                                .find(".ct_size1.ui-autocomplete-input.ui-state-default.ui-corner-all")
                                .val();
                        var type = $(this)
                                .find(".ct_type1.ui-autocomplete-input.ui-state-default.ui-corner-all")
                                .val();
                        if (!(!num || !size || !type)) {
                            model = (model == "") ? (num + "X" + size + "\'" + type)
                                    : (model + "/" + num + "X" + size + "\'" + type);
                        } else {
                        }
                        if (i != 0) {
                            $(this).remove();
                        }
                    });
        }

        //验证货物信息必填项
        var ct_num1 = $("#ct_num1").val();
        var ct_size1 = $("#ct_size1").val();
        var ct_type1 = $("#ct_type1").val();
        if (goodsName == "") {
            alert("请填写货物名称！");
            $("#goodsName").focus();
            return;
        } else if (weight == "") {
            alert("请填写每柜毛重！");
            $("#weight").focus();
            return;
        } else if ((ct_num1 == "") || ct_size1 == "" || ct_type1 == "") {
            alert("请填写柜量，柜型！");
            $("#ct_num1").focus();
            return;
        }

        //看是否为修改数据
        if (id) {
        } else {
            if (sameBooking() == true) {
                alert("所填写的货物和柜型重复！");
                return;
            }
            id = temp_goodsId;//新增则取序列号
        }
        var appendContent = "<td>" + goodsName + "</td><td>" + weight + "</td><td>" + volume
                            + "</td><td>" + model + "</td><td style='display:none;'>" + id + "</td>"
                            + "<td><a style='cursor: pointer;color: blue;margin: 10px;' onclick='delBookBusi(this);'>删除</a></td>";
        if (!$("#temp_goodsId").val() || $("#temp_goodsId").val() == "") { //为新添加数据
            $("#goodsList")
                    .append("<tr id='tr_goodsId_" + temp_goodsId
                            + "' onclick='edit(this);' class='pointer'>" + appendContent + "</tr>");
        } else { //临时修改数据
            $(temp_goodsObj).empty();
            $(temp_goodsObj).append(appendContent);
        }
        $("#newGoods li input:text").each(function (i) {
            this.value = "";
        });
        temp_goodsId++;
        $("#temp_goodsId").val("");
        $("#isModify").val("");
        if (temp_goodsObj) {
            $(temp_goodsObj).css("color", "black");
        }

        //总共需要的柜型柜量
        var tds = $("#goodsList tr:gt(0)").find("td:eq(3)");
        var containers = new Array();
        if (tds && tds.length > 0) {
            for (var i = 0; i < tds.length; i++) {
                var goods = $(tds[i]).text();
                if (goods) {
                    var goodses = goods.split("/");
                    for (var j = 0; j < goodses.length; j++) {
                        var types = goodses[j].split("X");
                        containers.push(types);
                    }
                }
            }
            var containerNum = {};
            for (var i = 0; i < containers.length; i++) {
                eval("var tmp=containerNum[\"" + containers[i][1] + "\"];");
                var newNum = 0;
                if (tmp) {
                    newNum = new Number(tmp) + new Number(containers[i][0]);
                } else {
                    newNum = new Number(containers[i][0]);
                }
                eval("containerNum[\"" + containers[i][1] + "\"]=" + newNum + ";");
            }
            var str = "";
            $.each(containerNum, function (i, n) {
                str += n + "X" + i + "/";
            });
            if (str) {
                str = str.substring(0, str.length - 1);
            }
// 			var cons1 = $(".ul1 tr td li .containerType2");
// 			alert(cons1.length);
// 			var fill_inputs1 = $(cons1[0]).find("input");
// 			alert(fill_inputs1.length);
// 			$(fill_inputs1[0]).val(str);
            $("#bookTotalType").val(str);
            $("#ct_size1").val("20");
            $("#ct_type1").val("HD");
        }
    }

    //编辑商品信息
    var temp_goodsObj = null;
    function edit(tr_obj) {
        //用于阻止删除时触发的编辑事件
        if (stopCopy == 1) {
            stopCopy = 0;
            return;
        }
        if (temp_goodsObj)
            $(temp_goodsObj).css("color", "black");
        temp_goodsObj = tr_obj;
        $(tr_obj).css("color", "red");
        if (tr_obj) {
            var tds = $(tr_obj).find("td");
            if (tds && tds.length > 0) {
                $("#goodsName").val($(tds[0]).text());
                $("#weight").val($(tds[1]).text());
                $("#volume").val($(tds[2]).text());
                $("#temp_goodsId").val($(tds[4]).text());
                var goods = $(tds[3]).text();
                var goodses = goods.split("/");
                for (var i = 0; i < goodses.length; i++) {
                    var types = goodses[i].split("X");
                    var num = types[0]; //数量
                    var combox = types[1].split("\'");
                    var size = combox[0]; //尺寸
                    var type = combox[1]; //类型
                    if (i == 0) {
                    } else {
// 						addCtType();
                        addCtbookType();
                    }

                    var cons = $("#containers1 li .containerType1");
                    var fill_inputs = $(cons[i]).find("input");
                    $(fill_inputs[0]).val(num);
                    $(fill_inputs[1]).val(size);
                    $(fill_inputs[2]).val(type);
                    /*if (types[1] == "20'HD") {
                     $("#model1").val(types[0]);
                     } else if (types[1] == "40'") {
                     $("#model2").val(types[0]);
                     } else if (types[1] == "40'HQ") {
                     $("#model3").val(types[0]);
                     } else if (types[1] == "45'") {
                     $("#model4").val(types[0]);
                     }*/
                }
            }
        }
    }

    function submit_booking() {
        // 拼装订舱商品信息
        var book_trs = $("#goodsList tr");
        if (book_trs.length > 1) {
            var books = $(book_trs[0]).find("td");
            var inputs = "";
            var index = 0;
            for (var i = 1; i < book_trs.length; i++) {
                var tr_tds = $(book_trs[i]).find("td");
                for (var j = 0; j < tr_tds.length; j++) {
                    if ($(books[j]).attr("lang")) {
                        var value = $(tr_tds[j]).text();
                        if (value == null || value == "" || value == "undefined") {
                            value = "";
                        }
                        inputs =
                                inputs + "<input type='hidden' name='book.goodses[" + index + "]."
                                + $(books[j])
                                        .attr("lang") + "' value=\"" + value + "\" />";
                    }
                }
                index++;
            }
            $("#book_input").html("");
            $("#book_input").append(inputs);
        }
    }
    $(document).ready(function () {
        var port = new Array();
        $.ajax({
                   url: "../base/dropdown-port", async: false, success: function (data) {
                var all = data.results;
                $.each(all, function (i, n) {
                    port.push({id: n[1], label: n[2]});
                });
            }
               });
        var st_port_id = $("#stPort_book_id").val();
        var ar_port_id = $("#arPort_book_id").val();
        if (st_port_id != null || st_port_id != undefined || ar_port_id != null || ar_port_id
                                                                                   != undefined) {
            $.each(port, function (i, n) {
                if (n.id == st_port_id) {
                    $("#stPort_book").val(n.label);
                }
                if (n.id == ar_port_id) {
                    $("#arPort_book").val(n.label);
                }
            });
        }
        //起始港下拉
        $("#stPort_book").autocomplete({
                                           source: function (request, response) {
                                               var matcher = new RegExp($.ui.autocomplete
                                                       .escapeRegex(request.term), "i");
                                               response($.grep(port, function (value) {
                                                   value = value.label;
                                                   return matcher.test(value);
                                               }));
                                           },
                                           minLength: 1,
                                           select: function (event, ui) {
                                               $(this).val(ui.item.label);
                                               $("#stPort_book_id").val(ui.item.id);
                                           },
                                           change: function (event, ui) {
                                               var stPort_book = $("#stPort_book").val();
                                               var stPort_book_id = $("#stPort_book_id").val();
                                               var isStPort = false;
                                               $.each(port, function (i, n) {
                                                   if ((stPort_book == n.label) && (stPort_book_id
                                                                                    == n.id)) {
                                                       isStPort = true;
                                                   }
                                               });
                                               if (!isStPort) {
                                                   alert("请在起始港下拉框中选择起始港!");
                                               }
                                           }
                                       });
        //目的港下拉
        $("#arPort_book").autocomplete({
                                           source: function (request, response) {
                                               var matcher = new RegExp($.ui.autocomplete
                                                       .escapeRegex(request.term), "i");
                                               response($.grep(port, function (value) {
                                                   value = value.label;
                                                   return matcher.test(value);
                                               }));
                                           },
                                           minLength: 1,
                                           select: function (event, ui) {
                                               $(this).val(ui.item.label);
                                               $("#arPort_book_id").val(ui.item.id);
                                           },
                                           change: function (event, ui) {
                                               var arPort_book_id = $("#arPort_book_id").val();
                                               var arPort_book = $("#arPort_book").val();
                                               var isArPort = false;
                                               $.each(port, function (i, n) {
                                                   if ((arPort_book == n.label) && (arPort_book_id
                                                                                    == n.id)) {
                                                       isArPort = true;
                                                   }
                                               });
                                               if (!isArPort) {
                                                   alert("请在目的港下拉框中选择目的港!");
                                               }
                                           }
                                       });
    });
</script>
